//
//  UIView+Base.swift
//  TheNewOne
//
//  Created by 孟子弘 on 2019/4/9.
//  Copyright © 2019年 mzh. All rights reserved.
//

import Foundation
import UIKit

extension UIView{
    func round(_ cornerRadius:CGFloat, rectCorners:UIRectCorner) {
        let maskPath = UIBezierPath.init(roundedRect: bounds, byRoundingCorners: rectCorners, cornerRadii: CGSize.init(width: cornerRadius, height: cornerRadius))
        let maskLayer = CAShapeLayer.init()
        maskLayer.frame = bounds
        maskLayer.path = maskPath.cgPath
        layer.mask = maskLayer
    }
    
    func findViewController() -> (UIViewController?) {
        var respondse = next
        while respondse != nil {
            if respondse?.isKind(of: UIViewController.classForCoder()) ?? false {
                
                return (respondse as! UIViewController)
                
            } else {
                
                respondse = respondse?.next
                
            }
        }
        
        return nil
    }
    
    func findNavigationController() -> (UINavigationController?) {
        var respondse = next
        while respondse != nil {
            if respondse?.isKind(of: UINavigationController.classForCoder()) ?? false {
                
                return (respondse as! UINavigationController)
                
            } else {
                
                respondse = respondse?.next
                
            }
        }
        
        return nil
    }
    
    func findTabBarController() -> (UITabBarController?) {
        var respondse = next
        while respondse != nil {
            if respondse?.isKind(of: UITabBarController.classForCoder()) ?? false {
                
                return (respondse as! UITabBarController)
                
            } else {
                
                respondse = respondse?.next
                
            }
        }
        
        return nil
    }
    
}
extension UIView {
//  圆角
    @IBInspectable var cornerRadius: CGFloat {
        get {
            return layer.cornerRadius
        } set {
            layer.masksToBounds = (newValue > 0)
            layer.cornerRadius = newValue
        }
    }
    //  边线宽度
    @IBInspectable var borderWidth: CGFloat {
        get {
            return layer.borderWidth
        } set {
            layer.borderWidth = newValue
        }
    }
    //  边线颜色
    @IBInspectable var borderColor: UIColor {
        get {
            return UIColor(cgColor: layer.borderColor!)
        } set {
            layer.borderColor = newValue.cgColor
        }
    }
}

